﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient; 
using System.Data;
using Stone.Library.DB;
using Stone.Library.Common;
using Stone.DataContracts; 

namespace CourseArraging.DAO
{
    public class LsapDAO
    {
        public bool Exists(int lsap_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from lsap");
            strSql.Append(" where lsap_id=@lsap_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@lsap_id", SqlDbType.Int,4)};
            parameters[0].Value = lsap_id;

            if (DBExecuteUtil.querySqlTable(strSql.ToString(), parameters).Rows.Count > 0)
                return true;
            else
                return false;
        }
         
        public void Add(LsapEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into lsap(");
            strSql.Append("lsap_xybh,lsap_jybh,lsap_km,lsap_kcsj,lsap_zt,lsap_bz)");
            strSql.Append(" values (");
            strSql.Append("@lsap_xybh,@lsap_jybh,@lsap_km,@lsap_kcsj,'正在授课',@lsap_bz)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@lsap_xybh", SqlDbType.VarChar,50),
					new SqlParameter("@lsap_jybh", SqlDbType.VarChar,50),
					new SqlParameter("@lsap_km", SqlDbType.VarChar,50),
					new SqlParameter("@lsap_kcsj", SqlDbType.VarChar,50),
					new SqlParameter("@lsap_bz", SqlDbType.VarChar,1000)};
            parameters[0].Value = model.lsap_xybh;
            parameters[1].Value = model.lsap_jybh;
            parameters[2].Value = model.lsap_km;
            parameters[3].Value = model.lsap_kcsj;
            parameters[4].Value = model.lsap_bz;

            DBExecuteUtil.executeSqlWithPar(strSql.ToString(), parameters);
            new Zap2DAO().Update2Busy(int.Parse(model.lsap_jybh.Substring(1)),
                "T" + CommonHelper.course2simple(model.lsap_kcsj));
        }
         
        public void Update(LsapEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update lsap set ");
            strSql.Append("lsap_xybh=@lsap_xybh,");
            strSql.Append("lsap_jybh=@lsap_jybh,");
            strSql.Append("lsap_km=@lsap_km,");
            strSql.Append("lsap_kcsj=@lsap_kcsj,");
            strSql.Append("lsap_zt=@lsap_zt");
            strSql.Append(" where lsap_id=@lsap_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@lsap_id", SqlDbType.Int,4),
					new SqlParameter("@lsap_xybh", SqlDbType.VarChar,50),
					new SqlParameter("@lsap_jybh", SqlDbType.VarChar,50),
					new SqlParameter("@lsap_km", SqlDbType.VarChar,50),
					new SqlParameter("@lsap_kcsj", SqlDbType.VarChar,500),
					new SqlParameter("@lsap_zt", SqlDbType.VarChar,50)};
            parameters[0].Value = model.lsap_id;
            parameters[1].Value = model.lsap_xybh;
            parameters[2].Value = model.lsap_jybh;
            parameters[3].Value = model.lsap_km;
            parameters[4].Value = model.lsap_kcsj;
            parameters[5].Value = model.lsap_zt;

            DBExecuteUtil.executeSqlWithPar(strSql.ToString(), parameters);
        }

        public void UpdateZT(LsapEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update lsap set ");
            strSql.Append("lsap_zt='已结束'");
            strSql.Append(" where lsap_jybh=@lsap_jybh and ");
            strSql.Append("lsap_km=@lsap_km and ");
            strSql.Append("lsap_kcsj=@lsap_kcsj ");
            SqlParameter[] parameters = {
					new SqlParameter("@lsap_jybh", SqlDbType.VarChar,50),
					new SqlParameter("@lsap_km", SqlDbType.VarChar,50),
					new SqlParameter("@lsap_kcsj", SqlDbType.VarChar,500)};
            parameters[0].Value = model.lsap_jybh;
            parameters[1].Value = model.lsap_km;
            parameters[2].Value = model.lsap_kcsj;

            DBExecuteUtil.executeSqlWithPar(strSql.ToString(), parameters);
            new Zap2DAO().Update2Idle(int.Parse(model.lsap_jybh.Substring(1)),
                "T" + CommonHelper.course2simple(model.lsap_kcsj));
        }
         
        public void Delete(int lsap_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from lsap ");
            strSql.Append(" where lsap_id=@lsap_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@lsap_id", SqlDbType.Int,4)};
            parameters[0].Value = lsap_id;

            DBExecuteUtil.executeSqlWithPar(strSql.ToString(), parameters);
        } 
         
        public LsapEntity GetModel(int lsap_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 lsap_id,lsap_xybh,lsap_jybh,lsap_km,lsap_kcsj,lsap_zt from lsap ");
            strSql.Append(" where lsap_id=@lsap_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@lsap_id", SqlDbType.Int,4)};
            parameters[0].Value = lsap_id;

            LsapEntity model = new LsapEntity();
            DataTable dt = DBExecuteUtil.querySqlTable(strSql.ToString(), parameters);
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["lsap_id"].ToString() != "")
                {
                    model.lsap_id = int.Parse(dt.Rows[0]["lsap_id"].ToString());
                }
                model.lsap_xybh = dt.Rows[0]["lsap_xybh"].ToString();
                model.lsap_jybh = dt.Rows[0]["lsap_jybh"].ToString();
                model.lsap_km = dt.Rows[0]["lsap_km"].ToString();
                model.lsap_kcsj = dt.Rows[0]["lsap_kcsj"].ToString();
                model.lsap_zt = dt.Rows[0]["lsap_zt"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
         
        public DataTable GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select lsap_id,lsap_xybh,lsap_jybh,lsap_km,lsap_kcsj,lsap_zt ");
            strSql.Append(" FROM lsap ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DBExecuteUtil.querySqlTable(strSql.ToString());
        }
         
        public DataTable GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" lsap_id,lsap_xybh,lsap_jybh,lsap_km,lsap_kcsj,lsap_zt ");
            strSql.Append(" FROM lsap ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DBExecuteUtil.querySqlTable(strSql.ToString());
        }
    }
}
